package com.cjjd.c.feature.home.chekin.checklist_detail;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cjjd.c.EventBusKeys;
import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarActivity;
import com.cjjd.c.bean.CheckInDetailBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.feature.dialog.Text2Dialog;
import com.cjjd.c.feature.dialog.TextDialog;
import com.cjjd.c.feature.home.chekin.self_select.SelfSelectHomeActivity;
import com.cjjd.c.feature.subscribe.change_order.ChangeOrderActivity;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.DateTimeUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/9/24.
 */

public class CheckinDetailActivity extends ActionBarActivity {

    @Bind(R.id.tv_order_form)
    TextView tvOrderForm;
    @Bind(R.id.tv_room_num)
    TextView tvRoomNum;
    @Bind(R.id.tv_order_person_name)
    TextView tvOrderPersonName;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_order_person_id)
    TextView tvOrderPersonId;
    @Bind(R.id.tv_time_arrive)
    TextView tvTimeArrive;
    @Bind(R.id.tv_time_for_settle_leave)
    TextView tvTimeForSettleLeave;
    @Bind(R.id.tv_hotel_style)
    TextView tvHotelStyle;
    @Bind(R.id.tv_order_id)
    TextView tvOrderId;
    @Bind(R.id.btn_next)
    Button btnNext;
    @Bind(R.id.tv_hotel_name)
    TextView tvHotelName;
    @Bind(R.id.tv_roomMoney)
    TextView tvRoomMoney;
    @Bind(R.id.tv_depositMoney)
    TextView tvDepositMoney;
    @Bind(R.id.tv_coupon)
    TextView tvCoupon;
    @Bind(R.id.tv_specialDemand)
    TextView tvSpecialDemand;
    @Bind(R.id.tv_receipt_need)
    TextView tvReceiptNeed;
    @Bind(R.id.tv_pay_mode)
    TextView tvPayMode;

    @Bind(R.id.ll_change_order)
    LinearLayout ll_change_order;

    private String orderMainUuid;
    private String orderDetailUuid;
    private CheckInDetailBean.Order order;

    private String tag = "CheckinDetailFragment";

    @Override
    public Object getTitleString() {
        return "订单信息";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_checkin_detail;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        orderMainUuid = getIntent().getStringExtra("mainUuid");
        orderDetailUuid = getIntent().getStringExtra("detailUuid");
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCheckinDetail();
    }

    private void getCheckinDetail() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("orderUuid", orderMainUuid);                 // 预订单主表uuid
        h1.put("orderDetailUuid", orderDetailUuid);         // 预订单明细uuid

        SharePrefUtil.saveString(mContext, ShareKeys.orderMainUuid, orderMainUuid);
        SharePrefUtil.saveString(mContext, ShareKeys.orderDetailUuid, orderDetailUuid);

        Call<HttpResult<CheckInDetailBean>> call = service.checkIn(JsonUtils.convertParam("checkIn", h1));
        call.enqueue(new CallbackImpl<CheckInDetailBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<CheckInDetailBean>> call, Response<HttpResult<CheckInDetailBean>> response) {
                order = response.body().getData().order;
                changeUI(order);
            }
        });
    }

    private void changeUI(CheckInDetailBean.Order order) {

        if (order == null) {
            return;
        }

        tvHotelName.setText(order.hotelName);
//        tvTimeForSettleLeave.setText("到离时间");
        tvTimeForSettleLeave.setText("入离店日期：" + DateTimeUtil.getNY(order.checkInDate) + "-" + DateTimeUtil.getNY(order.checkOutDate)
                + "  共" + order.checkInDayNum + "天");
        tvHotelStyle.setText(order.roomTypeName + "|" + order.breakfastTypeName);//+ "|" + order.bedInfo + "|" + order.windowInfo);
        tvOrderId.setText("订单编号: " + order.orderId);

        tvOrderForm.setText(order.orderSource);
        tvOrderPersonName.setText(order.reserveMan);
        tvRoomNum.setText(order.roomNum + "间");
        tvPhone.setText(order.reserveManMobile);
        tvOrderPersonId.setText(order.reserveManIdCard);
        tvTimeArrive.setText(order.arriveTime);


        if ("0".equals(order.roomPayType)) {
            tvRoomMoney.setText("未支付" + order.roomMoney + "元");
        } else if ("1".equals(order.roomPayType)) {
            tvRoomMoney.setText("已支付" + order.roomMoney + "元");
        }

        if ("0".equals(order.roomPayType)) {
            tvDepositMoney.setText("未支付" + order.depositMoney + "元");
        } else if ("1".equals(order.roomPayType)) {
            tvDepositMoney.setText("已支付" + order.depositMoney + "元");
        }

        tvCoupon.setText(order.discountInfo);
        tvSpecialDemand.setText(order.specialDemand);
        tvPayMode.setText("");
        tvReceiptNeed.setText("可能需要");

        // TODO: 2017/12/22 暂时取消，为了测试后面的流程
//        if ("2".equals(order.orderType)&&("1".equals(order.roomPayType)||"1".equals(order.depositPayType))) {
//            ll_change_order.setVisibility(View.VISIBLE);
//        }
        if ("2".equals(order.orderType)) {
            ll_change_order.setVisibility(View.VISIBLE);
        }


    }


    @Override
    public void initRightButton(Button rightButton) {
        rightButton.setVisibility(View.VISIBLE);
        rightButton.setText("取消订单");
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        ButterKnife.unbind(this);
        super.onDestroy();
    }

    @OnClick({R.id.btn_next, R.id.btn_change, R.id.btn_right})
    public void onViewClicked(View view) {

        switch (view.getId()) {

            case R.id.btn_next:

                // TODO: 2017/12/4 这里做时间判断是否直接弹出dialog
                new TextDialog(mContext, "亲爱的会员，您预定的房间只能在预订当日8点后才可以进行选房，到时我们会用短信通知您哦~", new TextDialog.OnCloseListener() {
                    @Override
                    public void OnClick() {
                        Intent intent = new Intent(mContext, SelfSelectHomeActivity.class);
                        intent.putExtra("mainUuid", orderMainUuid);
                        intent.putExtra("detailUuid", orderDetailUuid);
                        intent.putExtra("name",order.hotelName);
                        mContext.startActivity(intent);
                    }
                }).show();

                break;

            case R.id.btn_change:

                if (checkCanChange()) {
                    Intent intent1 = new Intent(mContext, ChangeOrderActivity.class);
                    intent1.putExtra("orderMainUuid", orderMainUuid);
                    mContext.startActivity(intent1);
                }


                break;

            case R.id.btn_right:
                if (checkCanCancel()) {
                    new Text2Dialog(mContext, "您确定要取消订单？", new Text2Dialog.OnCloseListener() {
                        @Override
                        public void OnClick() {
                            cancelBookOrder();
                        }

                        @Override
                        public void Onclose() {
                            //mContext.finish();
                        }
                    }).show();
                }
                break;
        }


    }

    private boolean checkCanChange() {
        if (TextUtils.isEmpty(orderMainUuid)) {
            ToastUtil.showShortToast(mContext, "非预定人不可取消订单");
            return false;
        }

        if (!"2".equals(order.orderType)) {
            ToastUtil.showShortToast(mContext, "非平台下单不能取消订单");
            return false;
        }

        if ("0".equals(order.roomPayType)) {
            ToastUtil.showShortToast(mContext, "未支付订单不能取消订单");
            return false;
        }
        return true;
    }


    /**
     * //1.到店时间两小时内不得再取消订单
     * //2.非预定人不可取消订单
     * //3.平台下单才能取消订单
     * <p>
     * 列表进入  平台下单  已支付 可以修改和取消
     * <p>
     * 列表进去 平台下单  可以取消
     *
     * @return
     */
    private boolean checkCanCancel() {

        if (TextUtils.isEmpty(orderMainUuid)) {
            ToastUtil.showShortToast(mContext, "非预定人不可取消订单");
            return false;
        }

        if (!"2".equals(order.orderType)) {
            ToastUtil.showShortToast(mContext, "非平台下单不能取消订单");
            return false;
        }
        return true;
    }


    /**
     * 取消订单
     */
    private void cancelBookOrder() {

        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("orderUuid", orderMainUuid);                 // 预订单主表uuid

        Call<HttpResult<String>> call = service.cancelBookOrder(JsonUtils.convertParam("cancelBookOrder", h1));
        call.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                Intent intent = new Intent(mContext, CancelOrderActivity.class);
                intent.putExtra("orderBean", order);
                mContext.startActivity(intent);
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {
        if (EventBusKeys.hadPayCheckIn.equals(event)) {
            finish();
        }
    }
}
